#pragma once
#include<iostream>

namespace Su
{
	template<typename T>
	class vector
	{
	public:
		typedef T* iterator;
		typedef const T* const_iterator;

		iterator begin()
		{
			return  _start;
		}
		iterator end()
		{
			return _finish;
		}

		const const_iterator begin() const
		{
			return  _start;
		}
		const const_iterator end() const
		{
			return _finish;
		}

		T& operator[](size_t pos)
		{
			assert(pos < size());
			return *(_start + pos);
		}

		const T& operator[](size_t pos) const
		{
			assert(pos < size());
			return *(_start + pos);
		}

		vector<T>& operator=(vector<T> v)//v是实参拷贝构造来的，而拷贝构造也实现了深层次拷贝，所以可以直接交换
		{
			swap(v);
			return *this;//退出函数时，v被销毁，_start指向的内容被v调用析构函数释放掉，最后v也被释放掉
		}

	private:
		T* _start;
		T* _finish;
		T* _endofstorage;
	public:
		//交换函数
		void swap(vector<T>& v)
		{
			std::swap(_start, v._start);
			std::swap(_finish, v._finish);
			std::swap(_endofstorage, v._endofstorage);
		}

		//获得有效数据
		size_t size() const
		{
			return _finish - _start;//_finish在有效数据的后一位
		}
		//打印
		void print() const
		{
			for (auto e : *this)
				std::cout << e << " ";
			std::cout << std::endl;
		}
		
		//获得容量
		size_t capacity() const
		{
			return _endofstorage - _start;
		}
		
		//拷贝构造
		vector(const vector<T>& v)
			:_start(nullptr)
			, _finish(nullptr)
			, _endofstorage(nullptr)
		{
			_start = new T[v.size()];
			for (size_t i = 0; i < v.size(); i++)
				_start[i] = v._start[i];//如果是string等自定义类型，就是调用这种类型的赋值操作（深拷贝）

			_finish = _start + v.size();
			_endofstorage = _start + v.capacity();
		}

		//默认构造函数
		vector(T* start = nullptr)
			:_start(start)
			,_finish(start)
			,_endofstorage(start)
		{}

		//构造函数
		vector(int n, const T& val = T()) 
		//int是为了防止类型匹配不上，跑到下面的构造函数去
		//例如：v(10,1)，如果跑到下面的代码则会报错
			:_start(nullptr)
			,_finish(nullptr)
			,_endofstorage(nullptr)
		{
			resize(n, val);
		}
		//构造函数（给两个迭代器）
		template<class InputIterator>
		vector(InputIterator first, InputIterator last)
		{
			while (first != last)
			{
				push_back(*first);
				++first;
			}
		}

		//析构函数
		~vector()
		{
			delete[] _start;
			_start = _finish = _endofstorage = nullptr;
		}



		//扩容
		void reserve(size_t n)
		{
			size_t capacity = size();
			if (capacity < n)
			{
				size_t len = _finish - _start;//记录扩容前，vector内的剩余量

				T*  tmp = new T[n];
				if (_start)
				{
					/*memcpy(tmp, _start, len * sizeof(T));
					std::swap(tmp, _start);*/

					for (size_t i = 0; i < size(); i++)
						tmp[i] = _start[i];//如果是string等自定义类型，就是调用这种类型的赋值操作（深拷贝）

					delete[] _start;
				}

				_start = tmp;
				_finish = _start + len;//将剩余量传回去
				_endofstorage = _start + n;
			}
		}

		void resize(size_t n, const T& val = T())
		{
			if (n < size())
			{
				size_t pos = _finish - _start;
				size_t pos2 = pos - n;
				_finish -= pos2;
			}
			else
			{
				reserve(n);
				while (_finish != _start + n)
				{
					*_finish = val;
					++_finish;
				}
			}

		}
		//尾插
		void push_back(T val)
		{
			if (_endofstorage == _finish)
			{
				size_t n = _endofstorage == nullptr ? 4 : capacity() * 2;
				reserve(n);
			}
			*_finish = val;
			_finish++;
		}

		iterator erase(iterator pos)
		{
			assert(pos >= _start && pos < _finish);

			iterator it = pos + 1;
			while (it != _finish)
			{
				*(it - 1) = *it;
				it++;
			}
			--_finish;
			return pos;
		}

	};
}